﻿import requests
from pyquery import PyQuery as pq
import smtplib
from email.mime.text import MIMEText
import time
def Fromsmil(conten):
    msg_from = 'xxxx@qq.com'  # 发送方邮箱
    passwd = 'xxxx'  # 填入发送方邮箱的授权码
    msg_to = 'xxxxx@qq.com'  # 收件人邮箱
    times=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    subject = '这是截止到{}疫情的相关相关报告'.format(times)  # 主题
    content = conten# 正文
    msg = MIMEText(content)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to
    try:
        s = smtplib.SMTP_SSL("smtp.qq.com", 465)# 邮件服务器及端口号
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg_to, msg.as_string())
        print("发送成功")
    except s.SMTPException as  e:
        print("发送失败")
    finally:
        s.quit()

demo=0
while True:
    url='https://news.163.com/special/epidemic/?spssid=3c6328541138b22dea91b6df3944aeaf&spsw=2&spss=qr'

    request=requests.get(url).text
    #解析
    doc=pq(request)

    item=doc('.cover_data').items()
    #数据的时间
    uy=doc('.cover_con .cover_li').text()
    #print(uy)
    for i in item:


        #确诊
        Diagnosis=int(i.find('.cover_confirm .number').text())

        #疑似

        Suspected=int(i.find('.cover_suspect .number').text())
        #疑似较昨日

        Doubtful_yesterday=int(i.find('.cover_suspect .added').text()[4:])
        #死亡

        death=int(i.find('.cover_dead .number').text())

        #治愈

        Cure=int(i.find('.cover_heal .number').text())

        #计算当天和前一天的
        add=Diagnosis-demo
        #算出概率
        Percentage = "%.2f%%" % (add / Diagnosis * 100)
        string=('这是{}数据\n'
                '共确诊:{}\n'
                '共疑似:{}\n'
                '与昨日疑似增加了:{}\n'
                '共死亡:{}\n'
                '共治愈:{}\n'
                '与前一条数据确诊:{}\n'
                '增加的概率：{}\n').format(uy,Diagnosis,Suspected,Doubtful_yesterday,death,Cure,add,Percentage)
        Fromsmil(string)
        print(string)
        demo = Diagnosis
        time.sleep(10800)







